package com.open.launcher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.open.launcher.R;
import com.open.launcher.base.MyApplication;
import com.open.launcher.presenter.AllChannelsPresenter;
import com.open.launcher.utils.APPLog;


/**
 * Created by Shinelon on 2017/9/15.
 */

public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.MainRecycleViewHolder> {
    private AllChannelsPresenter presenter;
    private int count;
    public int position;
    private RecyclerView parentView;
    private MainRecycleViewHolder currentHolder;
    private Context context;
    private Handler mainHandler;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            Toast.makeText(context, "滚动===", Toast.LENGTH_SHORT).show();
            for (int i = 0; i < parentView.getChildCount(); i++) {
                View childView = parentView.getChildAt(i);
                int position = parentView.getChildLayoutPosition(childView);
                MainRecycleViewHolder viewHolder = (MainRecycleViewHolder) parentView.getChildViewHolder(childView);
                APPLog.printInfo("TVodViewHolder:" + position);
                if (position == ProgramAdapter.this.position) {
                    currentHolder = viewHolder;
                    currentHolder.onFocusChange(true);
                    APPLog.printInfo("this.position:" + ProgramAdapter.this.position);
                    break;
                }
            }
        }
    };

    public ProgramAdapter(AllChannelsPresenter presenter, Context context) {
        this.presenter = presenter;
        this.context = context;
    }

    public void setCount(int count) {
        this.count = count;
    }
    public void setMainHandler(Handler handler){
        this.mainHandler = handler;
    }
    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_program_view, null, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        if (parentView == null) {
            parentView = (RecyclerView) parent;
            parentView.addOnScrollListener(new ChannelScrollListener());
        }
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindProgram(holder, position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public int selectedNext() {
        // if (position < count - 1) {
        unselected();
        position += 1;
        selected();
        // }
        return position;
    }

    public int selectedPre() {
        // if (position > 0) {
        unselected();
        position -= 1;
        selected();
        // }
        return position;
    }

    public void selected() {
        if (parentView == null) {
            return;
        }
        if (position >= count) {
            parentView.scrollToPosition(0);
            this.position = 0;
        } else if (position < 0) {
            parentView.scrollToPosition(count - 1);
            this.position = count - 1;
        } else {
            parentView.scrollToPosition(this.position);
        }
        handler.sendEmptyMessageDelayed(0, 200);
    }

    public void onClick() {
        currentHolder.onClick();
    }

    public void unselected() {
        if (currentHolder != null) {
            currentHolder.onFocusChange(false);
            currentHolder = null;
            APPLog.printInfo("channelAdapter :" + "unselected()" + position);
        }
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements AllChannelsPresenter.ProgramViewHolder {

        private TextView title_text;
        private TextView type_btn;
        private TextView start_time;
        private TextView end_time;
        private int position;

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            title_text = (TextView) itemView.findViewById(R.id.title_text);
            type_btn = (TextView) itemView.findViewById(R.id.type_btn);
            start_time = (TextView) itemView.findViewById(R.id.start_time);
            end_time = (TextView) itemView.findViewById(R.id.end_time);
        }

        public void onFocusChange(boolean hasFocus) {
            if (hasFocus) {
                itemView.setBackgroundResource(R.mipmap.dise_left);
                title_text.setSelected(true);
            } else {
                itemView.setBackgroundResource(0);
                title_text.setSelected(false);
            }
        }

        public void onClick() {
            Bundle bundle  = presenter.toPlayTvodProgram(position);
            Message message = Message.obtain();
            message.obj = bundle;
            message.what = 12;
            mainHandler.sendMessage(message);
        }

        private void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void setTitleText(String name) {
            title_text.setTypeface(MyApplication.face);
            title_text.setText(name);
        }

        @Override
        public void setTypeBtnText(String name, int type) {
            type_btn.setTypeface(MyApplication.face);
            if (type == 1) {
                type_btn.setBackgroundColor(Color.parseColor("#C65F48"));
            } else {
                type_btn.setBackgroundColor(Color.parseColor("#00ffffff"));
            }
            type_btn.setText(name);
        }

        @Override
        public void setStartTimeText(String name) {
            start_time.setTypeface(MyApplication.face);
            start_time.setText(name);
        }

        @Override
        public void setEndTimeText(String name) {
            end_time.setTypeface(MyApplication.face);
            end_time.setText(name);
        }
    }

    class ChannelScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {

            }
        }
    }
}
